<?php

class MyMultiCurl extends MultiCurl {

    public $siteUrl;
    public $cat_name;
    public $reg_exp_name;
    public $reg_exp_art;
    public $reg_exp_price;
    public $reg_exp_desc;
    public $reg_exp_har;
    public $reg_exp_img;
    public $check;
    public $name;
    public $art;
    public $price;
    public $desc;
    public $har;
    public $imgs;
    public $id;

    public function __construct($siteUrl, $cat_name, $reg_exp_name, $reg_exp_art, $reg_exp_price, $reg_exp_desc, $reg_exp_har, $check, $reg_exp_img) {
        parent::__construct();
        $this->siteUrl = $siteUrl;
        $this->cat_name = $cat_name;
        $this->reg_exp_name = $reg_exp_name;
        $this->reg_exp_art = $reg_exp_art;
        $this->reg_exp_price = $reg_exp_price;
        $this->reg_exp_desc = $reg_exp_desc;
        $this->reg_exp_har = $reg_exp_har;
        $this->check = $check;
        $this->reg_exp_img = $reg_exp_img;
        $this->reg_exp_charset = '#\<meta[^<>]+charset\s*\=\s*([\w\-]+)#';
        $this->id = 0;
    }

    private function clear($str) {
        $str = str_replace("\r\n", "", $str);
        $patterns = array('#<table.+>#Uis', '#<tr.+>#Uis', '#<td.+>#Uis', '#[\r\n]+#');
        $replacements = array("<table>", "<tr>", "<td>", " ");
        return trim(strip_tags(preg_replace($patterns, $replacements, $str), "<table></table><tbody></tbody><tr></tr><td></td>"));
    }

    protected function onLoad($url, $response, $info) {       
        if (preg_match($this->reg_exp_charset, $response, $m)) {
            $response = iconv($m[1], 'utf-8', $response);
        }
        if (preg_match($this->reg_exp_name, $response, $m)) {
            $this->name = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_art, $response, $m)) {
            $this->art = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_price, $response, $m)) {
            $this->price = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_desc, $response, $m)) {
            $this->desc = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_har, $response, $m)) {
            if ($this->check == "off") {
                $this->har = $this->clear($m[1]);
            } else {
                $this->har = $this->clear($m[0]);
            }
        }
        if (preg_match_all($this->reg_exp_img, $response, $m)) {           
            $this->imgs = array_unique($m[1]);
        }
        
        $res = new Result($this);
        $res->add();
        $this->id++;
    }

}
?>
